from header_common import *
from header_operations import *
from header_parties import *
from header_items import *
from header_skills import *
from header_triggers import *
from header_troops import *

from module_constants import *

####################################################################################################################
# Simple triggers are the alternative to old style triggers. They do not preserve state, and thus simpler to maintain.
#
#  Each simple trigger contains the following fields:
# 1) Check interval: How frequently this trigger will be checked
# 2) Operation block: This must be a valid operation block. See header_operations.py for reference. 
####################################################################################################################



simple_triggers = [
  (ti_on_party_encounter,
   [
       (store_encountered_party, "$g_encountered_party"),
       (store_encountered_party2,"$g_encountered_party_2"), # encountered_party2 is set when we come across a battle or siege, otherwise it's a minus value
       (assign, "$talk_context", 0),
       (assign,"$g_player_surrenders",0),
       (assign,"$g_enemy_surrenders",0),
       (assign, "$g_leave_encounter",0),
       (assign, "$new_encounter", 1), #check this in the menu.
       (try_begin),
         (lt, "$g_encountered_party_2",0), #Normal encounter. Not battle or siege.
         (try_begin),
           (is_between, "$g_encountered_party", towns_begin, towns_end),
           (jump_to_menu, "mnu_castle_outside"),
         (else_try),
           (is_between, "$g_encountered_party", castles_begin, castles_end),
           (jump_to_menu, "mnu_castle_outside"),
         (else_try),
           (eq, "$g_encountered_party", "p_zendar"),
           (jump_to_menu, "mnu_zendar"),
         (else_try),
           (eq, "$g_encountered_party", "p_salt_mine"),
           (jump_to_menu, "mnu_salt_mine"),
         (else_try),
           (eq, "$g_encountered_party", "p_four_ways_inn"),
           (jump_to_menu, "mnu_four_ways_inn"),
         (else_try),
           (eq, "$g_encountered_party", "p_dhorak_keep"),
           (jump_to_menu, "mnu_dhorak_keep"),
         (else_try),
           (eq, "$g_encountered_party", "p_training_ground"),
           (jump_to_menu, "mnu_training_ground"),
         (else_try),
           (jump_to_menu, "mnu_simple_encounter"),
         (try_end),
       (else_try), #Battle
         (try_begin),
           (this_or_next|is_between, "$g_encountered_party", towns_begin, towns_end),
           (is_between, "$g_encountered_party", castles_begin, castles_end),
           (try_begin),
             (eq, "$auto_enter_town", "$g_encountered_party"),
             (jump_to_menu, "mnu_town"),
           (try_end),
         (else_try),
           (jump_to_menu, "mnu_pre_join"),
         (try_end),
       (try_end),
       (assign,"$auto_enter_town",0),
    ]),


#Battle simulation
  (ti_simulate_battle,
   [
       (store_trigger_param_1, ":root_defender_party"),
       (store_trigger_param_2, ":root_attacker_party"),
       (party_collect_attachments_to_party, ":root_defender_party", "p_collective_ally"),
       (party_collect_attachments_to_party, ":root_attacker_party", "p_collective_enemy"),

       (call_script, "script_party_count_fit_regulars", "p_collective_ally"),
       (store_div, ":defender_strength", reg0, 20),
       (val_add, ":defender_strength", 1),
       (call_script, "script_party_count_fit_regulars", "p_collective_enemy"),
       (store_div, ":attacker_strength", reg0, 20),
       (val_add, ":attacker_strength", 1),

       (inflict_casualties_to_party_group, ":root_attacker_party", ":defender_strength", "p_temp_casualties"),
       (party_collect_attachments_to_party, ":root_attacker_party", "p_collective_enemy"),
       (call_script, "script_party_calculate_regular_strength", "p_collective_enemy"),
       (assign, ":new_attacker_strength", reg0),

       (try_begin),
         (gt, ":new_attacker_strength", 0),
         (inflict_casualties_to_party_group, ":root_defender_party", ":attacker_strength", "p_temp_casualties"),
       (try_end),
       (party_collect_attachments_to_party, ":root_defender_party", "p_collective_ally"),
       (call_script, "script_party_calculate_regular_strength", "p_collective_ally"),
       (assign, ":new_defender_strength", reg0),

       (try_begin),
         (this_or_next|eq, ":new_attacker_strength", 0),
         (eq, ":new_defender_strength", 0),
         # Battle concluded! determine winner

         (try_begin),
           (eq, ":new_attacker_strength", 0),
           (assign, ":root_winner_party",   ":root_defender_party"),
           (assign, ":root_defeated_party", ":root_attacker_party"),
           (assign, ":collective_casualties",    "p_collective_enemy"),
         (else_try),
           (assign, ":root_winner_party", ":root_attacker_party"),
           (assign, ":root_defeated_party",  ":root_defender_party"),
           (assign, ":collective_casualties",  "p_collective_ally"),
         (try_end),

         (call_script, "script_get_nonempty_party_in_group", ":root_winner_party"),
         (assign, ":nonempty_winner_party", reg0),
         (ge, ":nonempty_winner_party", 0),

         (store_faction_of_party, ":faction_receiving_prisoners", ":nonempty_winner_party"),
         (store_faction_of_party, ":defeated_faction", ":root_defeated_party"),

         (party_clear, "p_temp_party"),
         (assign, "$g_move_heroes", 0), #heroes are already processed above. Skip them here.
         (call_script, "script_party_add_party_prisoners", "p_temp_party", ":collective_casualties"),
         (call_script, "script_party_prisoners_add_party_companions", "p_temp_party", ":collective_casualties"),
         (distribute_party_among_party_group, "p_temp_party", ":root_winner_party"),
         (call_script, "script_clear_party_group", ":root_defeated_party"),
         (set_trigger_result, 1), #End battle!
         (try_begin),
           (eq, ":root_defeated_party", 0),
           (display_message,"str_error_string"),
         (try_end),
      (try_end),

       (store_random_in_range, ":random_num",0, 100),
       (try_begin),
         (lt, ":random_num", 5),
         (set_trigger_result, 1), #End battle!
       (try_end),
    ]),

#Auto-menu
  (0,
   [
       (map_free),
       (ge,"$auto_menu",1),
       (jump_to_menu,"$auto_menu"),
       (assign,"$auto_menu",-1),
    ]),

  (0,
   [
       (map_free),
       (ge,"$auto_enter_town",1),
       (start_encounter, "$auto_enter_town"),
    ]),

#####!!!!!
]

